package com.imc.platform.base.sign.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.imc.platform.base.org.service.IOrgService;
import com.imc.platform.base.sign.pojo.Sign;
import com.imc.platform.base.sign.service.IPlatSignService;
import com.imc.platform.base.user.dao.mapper.IUserMapper;
import com.imc.platform.base.user.pojo.User;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/*
 * 类描述：签到控制器类
 * 开发者：YT
 * 创建日期：2018/10/27 22:35
 */
@Controller
@RequestMapping("/base/sign")
public class PlatSignController {

    @Autowired
    IPlatSignService signService;

    @Autowired
    IOrgService iOrgService;

    @Autowired
    private IUserMapper userMapper;

    /**
     * QY 2018/9/3 8:01
     * 方法描述：传参对象
     **/
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class ReqTuple {
        Map <String, String> parmsMap;
    }

    /*
     *  sjc 2018-8-17 10:01:50
     *  方法描述：公告页面跳转
     */
    @GetMapping("/signQuery")
    public String signQuery() {
        return "base/sign/sign";
    }

    /*
     * YT  2018/9/14 13:49
     * 方法描述：查询分类列表
     */
    @RequestMapping(value = "/querySignList", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO querySignList(Principal principal, Pageable pageable,@RequestParam String signerId,@RequestParam String studentId,@RequestParam String status,@RequestParam String scheduleId,@RequestParam String lessonId,@RequestParam String startTime,@RequestParam String endTime) {
        String uname = principal.getName();
        User user = userMapper.queryRoleUserByLoginName(uname);
        String orgId = iOrgService.queryOrgIdByUserId(String.valueOf(user.getId()));
        Map <String, String> mapP = new HashMap<>();
        mapP.put("signerId",signerId);
        mapP.put("studentId",studentId);
        mapP.put("status",status);
        mapP.put("scheduleId",scheduleId);
        mapP.put("lessonId",lessonId);
        mapP.put("startTime",startTime);
        mapP.put("endTime",endTime);
        //4是老师
        if("master".equals(user.getDuty())){
            mapP.put("dutyFlag", "1");
        }
        if("branch".equals(user.getDuty())){
            mapP.put("dutyFlag", "2");
        }
        if("teacher".equals(user.getDuty())){
            mapP.put("dutyFlag", "3");
        }
        if("admin".equals(user.getDuty())){
            mapP.put("dutyFlag", "0");
        }
        mapP.put("orgId", orgId);
        //4是老师
        mapP.put("roleId",user.getRoleId());
        mapP.put("currentUserId",String.valueOf(user.getId()));
        PageInfo <Sign> querySignList = signService.querySignList(pageable.getPageNumber(), pageable.getPageSize(),mapP);
        for (Sign c : querySignList.getList()) {
            c.setPageNumber(pageable.getPageNumber());
            c.setPageSize(pageable.getPageSize());
        }
        JSONObject map = new JSONObject();
        map.put("total", querySignList.getTotal());
        map.put("rows", querySignList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

    /**
     * QY 2018/9/3 10:16
     * 方法描述：查询所有用户
     **/
    @RequestMapping(value = "/querySignByMap", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO querySignByMap(Principal principal,@RequestBody ReqTuple reqTuple) {
        String uname = principal.getName();
        User user = userMapper.queryRoleUserByLoginName(uname);
        String orgId = iOrgService.queryOrgIdByUserId(String.valueOf(user.getId()));
        Map <String, String> mapP = reqTuple.getParmsMap();
        //4是老师
        if("master".equals(user.getDuty())){
            mapP.put("dutyFlag", "1");
        }
        if("branch".equals(user.getDuty())){
            mapP.put("dutyFlag", "2");
        }
        if("teacher".equals(user.getDuty())){
            mapP.put("dutyFlag", "3");
        }
        mapP.put("orgId", orgId);
        //4是老师
        mapP.put("roleId",user.getRoleId());
        mapP.put("currentUserId",String.valueOf(user.getId()));
        PageInfo <Sign> queryList = signService.querySignByMap(Integer.parseInt(reqTuple.getParmsMap().get("page")), Integer.parseInt(reqTuple.getParmsMap().get("size")), mapP);
        for (Sign c : queryList.getList()) {
            c.setPageNumber(Integer.parseInt(reqTuple.getParmsMap().get("page")));
            c.setPageSize(Integer.parseInt(reqTuple.getParmsMap().get("size")));
        }
        JSONObject map = new JSONObject();
        map.put("total", queryList.getTotal());
        map.put("rows", queryList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

}
